package cat.sun.adapters;

import java.util.ArrayList;
import java.util.List;

import cat.sun.R;
import cat.sun.themes.ThemeConstants;
import cat.sun.themes.ThemeName;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ThemeDialogAdapter extends BaseAdapter 
{
	private LayoutInflater inflater;
	private Context context;
	private int currentChecked;
	private List<ImageHolder> images;
	
	public ThemeDialogAdapter(Context activity, int currentChecked)
	{
		this.inflater = LayoutInflater.from(activity);
		this.context = activity;
		this.currentChecked = currentChecked;	
		images = new ArrayList<ImageHolder>();
		ThemeName [] themeName = ThemeName.values();
		for (ThemeName theme:themeName)
		{
			ImageHolder holder= new ImageHolder();
			holder.name = theme;
			holder.img1 = context.getResources().getIdentifier(theme.toString()+"_"+"bomb", "drawable", context.getPackageName());
			holder.img2 = context.getResources().getIdentifier(theme.toString()+"_"+"open", "drawable", context.getPackageName());
			holder.img3 = context.getResources().getIdentifier(theme.toString()+"_"+"close", "drawable", context.getPackageName());
			holder.img4 = context.getResources().getIdentifier(theme.toString()+"_"+"one", "drawable", context.getPackageName());
			images.add(holder);
		}
	}
	
	@Override
	public int getCount() 
	{
		return ThemeName.values().length;
	}

	@Override
	public Object getItem(int arg0) 
	{
		return null;
	}

	@Override
	public long getItemId(int position) 
	{
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) 
	{
		if (convertView == null)  convertView = this.inflater.inflate(R.layout.item_dialog_themes, null);
		
		TextView titleView = (TextView) convertView.findViewById(R.id.dialog_themes_title);

		titleView.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf"));
		
		ImageView im1 = (ImageView) convertView.findViewById(R.id.dialog_theme_pic1);
		ImageView im2 = (ImageView) convertView.findViewById(R.id.dialog_theme_pic2);
		ImageView im3 = (ImageView) convertView.findViewById(R.id.dialog_theme_pic3);
		ImageView im4 = (ImageView) convertView.findViewById(R.id.dialog_theme_pic4);
		
		final ImageView check = (ImageView) convertView.findViewById(R.id.dialog_theme_chcked);

		im1.setImageResource(images.get(position).img1);
		im2.setImageResource(images.get(position).img2);
		im3.setImageResource(images.get(position).img3);
		im4.setImageResource(images.get(position).img4);
						
		titleView.setText(images.get(position).name.getOficialname());		
		
		if (currentChecked == position) 
		{
			check.setImageResource(R.drawable.saper_check);
		}
		else check.setImageBitmap(null);
				
		convertView.setOnClickListener(new OnClickListener() 
		{
			
			@Override
			public void onClick(View v) 
			{								
				CommitThread commitThread = new CommitThread(position);
				commitThread.start();	
				currentChecked = position;
				notifyDataSetChanged();		
			}
		});
		return convertView;
	}
	
	class CommitThread extends Thread
	{
		private int pos;
		
		public CommitThread(int pos)
		{
			this.pos = pos;
		}
		
		@Override
		public void run() 
		{
			ThemeName [] names = ThemeName.values();
			SharedPreferences sharedPreferences  = context.getSharedPreferences(ThemeConstants.bundleTag, 0);
			Editor editor = sharedPreferences.edit();
			editor.putString(ThemeConstants.savedTheme, names[this.pos].toString());
			editor.commit();
		}
	};
	
	class ImageHolder
	{
		public ThemeName name;
		public int img1;
		public int img2;
		public int img3;
		public int img4;		
	}

}
